package com.librelato.webservices;

import java.net.URL;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.xml.namespace.QName;

import org.apache.axis.AxisEngine;
import org.apache.axis.AxisFault;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.client.Stub;
import org.apache.axis.constants.Style;
import org.apache.axis.constants.Use;
import org.apache.axis.description.OperationDesc;
import org.apache.axis.description.ParameterDesc;
import org.apache.axis.encoding.ser.BeanDeserializerFactory;
import org.apache.axis.encoding.ser.BeanSerializerFactory;
import org.apache.axis.soap.SOAPConstants;

import com.librelato.persistencia.config.PropriedadesDeConfiguracao;
import com.librelato.persistencia.config.PropriedadesDeConfiguracaoEnum;

public class WsSoapStubManutencao extends Stub {

	private String ip = PropriedadesDeConfiguracao.getInstance()
			.getValorPropriedade(PropriedadesDeConfiguracaoEnum.WSDL_IP);

	public String inclui(DadosManu oSolici) throws Exception {
		String ret = "";
		try {
			Service service = new Service(new QName(ip
					+ "sol/WSSOLMANUTENCAO.apw", "WSSOLMANUTENCAO"));
			service.setTypeMappingVersion("1.2");
			OperationDesc oper = new OperationDesc();
			ParameterDesc param = new ParameterDesc();
			param = new org.apache.axis.description.ParameterDesc(
					new javax.xml.namespace.QName(ip, "OSOLICI"),
					org.apache.axis.description.ParameterDesc.IN,
					new javax.xml.namespace.QName(ip, "DADOSMANU"),
					DadosManu.class, false, false);
			oper.setName("INCSOLMANU");
			oper.addParameter(param);
			oper.setReturnType(new QName("http://www.w3.org/2001/XMLSchema",
					"string"));
			oper.setReturnClass(String.class);
			oper.setReturnQName(new QName(ip, "INCSOLMANURESULT"));
			oper.setStyle(Style.WRAPPED);
			oper.setUse(Use.LITERAL);

			Call call = (Call) service.createCall();
			call.setTargetEndpointAddress(new URL(ip
					+ "sol/WSSOLMANUTENCAO.apw"));
			call.setPortName(new QName("WSSOLMANUTENCAOSOAP"));
			call.setUseSOAPAction(true);
			call.setSOAPActionURI(ip + "INCSOLMANU");
			call.setEncodingStyle(null);
			call.setProperty(Call.SEND_TYPE_ATTR, Boolean.FALSE);
			call.setProperty(AxisEngine.PROP_DOMULTIREFS, Boolean.FALSE);
			call.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
			call.setOperationName(new QName(ip, "INCSOLMANU"));
			call.setOperation(oper);
			call.registerTypeMapping(DadosManu.class,
					new QName(ip, "DADOSMANU"), BeanSerializerFactory.class,
					BeanDeserializerFactory.class);
			setRequestHeaders(call);
			setAttachments(call);

			ret = (String) call.invoke(new Object[] { oSolici });
		} catch (AxisFault e) {
			FacesMessage message = new FacesMessage(
					FacesMessage.SEVERITY_FATAL, "Erro",
					"Falha de Conexão com o Microsiga");
			FacesContext.getCurrentInstance().addMessage(null, message);
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return ret;
	}
}
